package com.lanou3g.springmvc.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 此Controller专门用来学习各种参数传递的方式
 */
@Controller
@RequestMapping("/param")
public class TestParamController {

    /**
     * 请求参数的传递
     * @return
     */
    @RequestMapping("/req_param")
    public String requestParamShow(@RequestParam String sname, Model model) {
        model.addAttribute("sname", sname);
        model.addAttribute("type", "请求参数");
        return "param_show";
    }

    /**
     * 请求头参数的传递
     * 最好配合postman来请求
     * @return
     */
    @RequestMapping("/req_header")
    public String requestHeaderParamShow(@RequestHeader String sname, Model model) {
        model.addAttribute("sname", sname);
        model.addAttribute("type", "请求头参数");
        return "param_show";
    }

    /**
     * 请求作用域中的参数的传递
     * @return
     */
    @RequestMapping("/req_scope")
    public String requestScopeParamShow(@RequestAttribute String sname, Model model) {
        model.addAttribute("sname", sname);
        model.addAttribute("type", "请求作用域参数");
        return "param_show";
    }

    /**
     * 会话作用域中的参数的传递
     * @return
     */
    @RequestMapping("/session_scope")
    public String sessionScopeParamShow(@SessionAttribute String sname, Model model) {
        model.addAttribute("sname", sname);
        model.addAttribute("type", "会话作用域参数");
        return "param_show";
    }

    /**
     * Cookie中的参数的传递
     * @return
     */
    @RequestMapping("/cookie")
    public String cookieParamShow(@CookieValue String sname, Model model) {
        model.addAttribute("sname", sname);
        model.addAttribute("type", "cookie参数");
        return "param_show";
    }


    @RequestMapping("/checkbox_param")
    public String getCheckboxParam(@RequestParam("hobby")String hobbyStr, Model model) {
//        String hobbyStr = Arrays.toString(hobby.toArray());
        model.addAttribute("sname", hobbyStr);
        model.addAttribute("type", "checkbox的值");
        return "param_show";
    }
}
